PYTHON25="c:\\python25\\python.exe"
PYTHON26="c:\\python26\\python.exe"
PYTHON30="c:\\python30\\python.exe"
DMD="dmd -w -release -O -inline"
TXT2TAGS="txt2tags"
PATCHOPT="-p1 -i misc/python3.patch"

# make "sh" verbose by default
if RakeFileUtils.verbose_flag==:default
  verbose(true)
end

require 'rake/clean'
CLOBBER.include("*.pyc")

desc "build depro.exe"
task :depro => "depro.exe"
CLOBBER.include("depro.exe")

file "depro.exe" => ["depro/depro.d", "depro/bignum.d"] do |t|
  sh "#{DMD} -of#{t.name} #{t.prerequisites.join(' ')}"
end
CLEAN.include("*.obj", "*.map")

desc "build documents"
task :doc => ["doc/tybc.html","doc/epro.html"]
CLOBBER.include("doc/*.html")

rule '.html' => '.t2t' do |t|
  sh "#{TXT2TAGS} #{t.source}"
end

desc "make release archive"
task :release => [:depro, :doc] do
  rmtree "release"
  puts "*** build release packgage to release/ ***"
  mkdir_p "release/depro"
  mkdir "release/doc"
  mkdir "release/sample"
  mkdir "release/sample_tape"
  mkdir "release/misc"
  cp "depro.exe", "release"
  cp "Rakefile", "release", {:preserve => true}
  cp Dir.glob("*.py"), "release", {:preserve => true}
  cp Dir.glob("depro/*"), "release/depro", {:preserve => true}
  cp Dir.glob("doc/*"), "release/doc", {:preserve => true}
  cp Dir.glob("sample/*"), "release/sample", {:preserve => true}
  cp Dir.glob("sample_tape/*"), "release/sample_tape", {:preserve => true}
  cp Dir.glob("misc/*"), "release/misc", {:preserve => true}
end
CLOBBER.include("release")

def test(python, dir)
  rmtree dir
  mkdir dir
  puts "** compile all sample **"
  samples=FileList["sample/*.txt"]
  codes=[]
  samples.each {|f|
    c=dir+'/'+File.basename(f,".txt")+'.code'
    sh "#{python} tybc.py #{f} -o #{c}"
    codes << c
  }
  codes.sort!
  sh "#{python} tybc.py -t sample_tape/furui2.txt -o #{dir}/furui2.codet"
  puts "** run all code on eprovm.py **"
  codes.each {|f|
    sh "#{python} eprovm.py #{f} >> #{dir}/py.out"
  }
  sh "echo -100 | #{python} eprovm.py -ts #{dir}/furui2.codet >> #{dir}/py.out"
  puts "** run all code on depro.exe **"
  codes.each {|f|
    sh "depro.exe #{f} >> #{dir}/d.out"
  }
  sh "echo -100 | depro.exe -t -s #{dir}/furui2.codet >> #{dir}/d.out"
  puts "** comparing outputs... **"
  unless cmp("misc/test.out", "#{dir}/py.out") and cmp("misc/test.out", "#{dir}/d.out")
    fail "output is differ!!"
  end
end

desc "run all test"
task :test => [:test25, :test26, :test30]

desc "test on python2.5"
task :test25 => :depro do
  puts "*** test on python2.5 ***"
  test(PYTHON25, "test25")
end
CLEAN.include("test25")

desc "test on python2.6"
task :test26 => :depro do
  puts "*** test on python2.6 ***"
  test(PYTHON26, "test26")
end
CLEAN.include("test26")

desc "test on python3.0"
task :test30 => :depro do
  puts "*** test on python3.0 ***"
  sh "patch #{PATCHOPT}"
  begin
    test(PYTHON30, "test30")
  ensure
    sh "patch #{PATCHOPT} -R"
  end
end
CLEAN.include("test30")
